from pyecharts.globals import SymbolType, ThemeType
from pyecharts import options as opts
import pandas as pd
import matplotlib.pyplot as plt
from pyecharts.charts import Line,Bar,PictorialBar,Pie


#显示中文
plt.rcParams['font.family'] = ['sans-serif']
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
# 解决保存图像是负号'-'显示为方块的问题
plt.rcParams['axes.unicode_minus'] = False
from pyecharts.faker import Faker

def totalnumpage():
    '''
    主题2 人口总数
    '''
    pwork = pd.read_excel('data/人口及劳动力历史变化.xlsx')
    pwork = pwork[~pwork['劳动力(万人)'].isnull()]
    pwork[['指标','年末总人口(万人)','劳动力(万人)']]
    pwork = pwork.reindex(index=pwork.index[::-1]) #逆序输出
    pwork['劳动力占比'] = pwork['劳动力(万人)']/pwork['年末总人口(万人)']
    x = pwork['指标']
    y1 = pwork['年末总人口(万人)']
    c = (
        Line()
        .add_xaxis(xaxis_data=x)
        .add_yaxis(
            series_name="全国总人口",
            stack="总量",
            y_axis=y1,
            label_opts=opts.LabelOpts(is_show=False),
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="中国总人口历史变化"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
    )
    return c.render('templates/中国总人口历史变化.html')

def laodonglinumpage():
    '''
    主题2 劳动力总数
    '''
    pwork = pd.read_excel('data/人口及劳动力历史变化.xlsx')
    pwork = pwork[~pwork['劳动力(万人)'].isnull()]
    pwork[['指标','年末总人口(万人)','劳动力(万人)']]
    pwork = pwork.reindex(index=pwork.index[::-1]) #逆序输出
    pwork['劳动力占比'] = pwork['劳动力(万人)']/pwork['年末总人口(万人)']
    pwork['劳动力占比'] = pwork['劳动力(万人)'] / pwork['年末总人口(万人)']
    x = pwork['指标']
    y2 = pwork['劳动力(万人)']
    y3 = pwork['劳动力占比']
    c = (
        Line()
            .add_xaxis(xaxis_data=x)
            .add_yaxis(
            series_name="劳动力人口",
            stack="总量",
            y_axis=y2,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="中国劳动力人口历史变化"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
    )
    return c.render('templates/中国劳动力人口历史变化.html')

def total():
    '''
    主题2 劳动力总数
    '''
    pwork = pd.read_excel('data/人口及劳动力历史变化.xlsx')
    pwork = pwork[~pwork['劳动力(万人)'].isnull()]
    pwork[['指标','年末总人口(万人)','劳动力(万人)']]
    pwork = pwork.reindex(index=pwork.index[::-1]) #逆序输出
    pwork['劳动力占比'] = pwork['劳动力(万人)']/pwork['年末总人口(万人)']
    pwork['劳动力占比'] = pwork['劳动力(万人)'] / pwork['年末总人口(万人)']
    x = pwork['指标']
    y1 = pwork['年末总人口(万人)']
    y2 = pwork['劳动力(万人)']
    y3 = pwork['劳动力占比']
    c = (
        Line()
            .add_xaxis(xaxis_data=x)
            .add_yaxis(
            series_name="全国总人口",
            stack="总量",
            y_axis=y1,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .add_yaxis(
            series_name="劳动力人口",
            stack="总量",
            y_axis=y2,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .add_yaxis(
            series_name="劳动力占比",
            stack="总量",
            y_axis=y3,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="三大产业就业趋势"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
    )
    return c.render("templates/中国劳动力变化折线图堆叠.html")

def nanpage():
    '''
    主题3 男性人口总数
    '''
    pwork = pd.read_excel('data/人口及劳动力历史变化.xlsx')
    x = pwork['指标']
    y1 = pwork['男性人口(万人)']

    c = (
        Line()
            .add_xaxis(xaxis_data=x)
            .add_yaxis(
            series_name="男性人口",
            stack="总量",
            y_axis=y1,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="中国男性人口历史变化"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
    )
    return c.render('templates/中国男性人口历史变化.html')

def nvpage():
    '''
    主题3 女性人口总数
    '''
    pwork = pd.read_excel('data/人口及劳动力历史变化.xlsx')
    x = pwork['指标']
    y2 = pwork['女性人口(万人)']

    c = (
        Line()
            .add_xaxis(xaxis_data=x)
            .add_yaxis(
            series_name="女性人口",
            stack="总量",
            y_axis=y2,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="中国女性人口历史变化"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
    )
    return c.render('templates/中国女性人口历史变化.html')

def progdp():
    '''
    主题4 各省GDP总数
    '''
    gdp = pd.read_csv('data/1992-2020年中国各省份分行业GDP增加值.csv')
    gdp.groupby('region')['gdp'].apply(lambda x: x.sum() / 10000).reset_index().sort_values(by='gdp',ascending=False).head(10)
    gdp_top10 = gdp.groupby('region')['gdp'].apply(lambda x: x.sum() / 10000).reset_index().sort_values(by='gdp',ascending=False).head(10)

    c = (
        PictorialBar()
            .add_xaxis(list(gdp_top10['region']))
            .add_yaxis(
            "",
            list(gdp_top10['gdp']),
            label_opts=opts.LabelOpts(is_show=False),
            symbol_size=18,
            symbol_repeat="fixed",
            symbol_offset=[0, 0],
            is_symbol_clip=True,
            symbol=SymbolType.ROUND_RECT,
        )
            .reversal_axis()
            .set_global_opts(
            title_opts=opts.TitleOpts(title="1992-2020年Top10省GDP增加值数据"),
            xaxis_opts=opts.AxisOpts(is_show=False),
            yaxis_opts=opts.AxisOpts(
                axistick_opts=opts.AxisTickOpts(is_show=False),
                axisline_opts=opts.AxisLineOpts(
                    linestyle_opts=opts.LineStyleOpts(opacity=0)
                ),
            ),
        )
    )
    return c.render('templates/1992-2020年Top10省GDP增加值数据.html')

def indgdp():
    '''
    主题4 各行业GDP总数
    '''
    gdp = pd.read_csv('data/1992-2020年中国各省份分行业GDP增加值.csv')
    gdp.groupby('industry')['gdp'].apply(lambda x: x.sum() / 10000).reset_index().sort_values(by='gdp',ascending=False).head(10)
    gdp_top10 = gdp.groupby('industry')['gdp'].apply(lambda x: x.sum() / 10000).reset_index().sort_values(by='gdp',ascending=False).head(10)
    x = list(gdp_top10['industry'])
    y = list(gdp_top10['gdp'])
    c = (
        Bar({"theme": ThemeType.MACARONS})
            .add_xaxis(x)
            .add_yaxis("GDP", y)
            .set_global_opts(
            title_opts={"text": "1992-2020TOP10行业GDP增加值", "subtext": "堆叠柱状图"}
        ))
    line = (
        Line()
            .set_global_opts(
            tooltip_opts=opts.TooltipOpts(is_show=False),
            xaxis_opts=opts.AxisOpts(type_="category"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
        )
            .add_xaxis(xaxis_data=x)
            .add_yaxis(
            series_name="1992-2020TOP10行业GDP增加趋势",
            y_axis=y,
            symbol="emptyCircle",
            is_symbol_show=True,
            label_opts=opts.LabelOpts(is_show=False),
        ))
    return c.overlap(line).render('templates/1992-2020TOP10行业GDP增加值数据堆叠.html')

def woker():
    '''
    主题5 三大产业分就业人员分布
    '''
    industry = pd.read_excel('data/按三大产业分就业人员分布 1952-2020.xlsx')
    industry = industry.reindex(index=industry.index[::-1])  # 逆序输出
    x = list(industry['指标'])
    y1 = list(industry['第一产业就业人员(万人)'])
    y2 = list(industry['第二产业就业人员(万人)'])
    y3 = list(industry['第三产业就业人员(万人)'])

    c = (
        Line()
            .add_xaxis(xaxis_data=x)
            .add_yaxis(
            series_name="第一产业就业人员",
            stack="总量",
            y_axis=y1,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .add_yaxis(
            series_name="第二产业就业人员",
            stack="总量",
            y_axis=y2,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .add_yaxis(
            series_name="第三产业就业人员",
            stack="总量",
            y_axis=y3,
            label_opts=opts.LabelOpts(is_show=False),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(title="三大产业就业趋势"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        ))
    return c.render('templates/三大产业就业折线图堆叠.html')

def industry():
    '''
    主题5 三大产业就业占比
    '''
    industry = pd.read_excel('data/按三大产业分就业人员分布 1952-2020.xlsx')
    industry = industry.reindex(index=industry.index[::-1])  # 逆序输出
    industry['第一产业就业人员占比'] = industry['第一产业就业人员(万人)'] / industry['就业人员(万人)']
    industry['第二产业就业人员占比'] = industry['第二产业就业人员(万人)'] / industry['就业人员(万人)']
    industry['第三产业就业人员占比'] = industry['第三产业就业人员(万人)'] / industry['就业人员(万人)']
    y1 = industry['第一产业就业人员占比'].mean()
    y2 = industry['第二产业就业人员占比'].mean()
    y3 = industry['第三产业就业人员占比'].mean()

    x_data = ['第一产业就业人员', '第二产业就业人员', '第三产业就业人员']
    y_data = [y1, y2, y3]
    data_pair = [list(z) for z in zip(x_data, y_data)]
    data_pair.sort(key=lambda x: x[1])

    c = (
        Pie(init_opts=opts.InitOpts(width="1000px", height="500px", bg_color="#b7d07a"))
            .add(
            series_name="数据",
            data_pair=data_pair,
            rosetype="radius",
            radius="55%",
            center=["50%", "50%"],
            label_opts=opts.LabelOpts(is_show=False, position="center"),
        )
            .set_global_opts(
            title_opts=opts.TitleOpts(
                title="三大产业占比",
                pos_left="center",
                pos_top="20",
                title_textstyle_opts=opts.TextStyleOpts(color="#fff"),
            ),
            legend_opts=opts.LegendOpts(is_show=False),
        )
            .set_series_opts(
            tooltip_opts=opts.TooltipOpts(
                trigger="item", formatter="{a} <br/>{b}: {c} ({d}%)"
            ),
            label_opts=opts.LabelOpts(color="#fff"),
        ))
    return c.render('templates/三大产业占比.html')

def cityzx():
    '''
    主题6 城镇经营
    '''
    doing = pd.read_excel('data/2004-2019年城镇私营与非私营就业人口分布.xlsx')
    doing = doing.reindex(index=doing.index[::-1])  # 逆序输出
    x = list(doing['指标'])
    y1 = list(doing['私营'])
    y2 = list(doing['非私营'])
    c = (
        Bar()
            .add_xaxis(x)
            .add_yaxis("私营", y1, stack="stack1")
            .add_yaxis("非私营", y2, stack="stack1")
            .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
            .set_global_opts(title_opts=opts.TitleOpts(title="2004-2019年城镇私营与非私营就业人口分布"))
    )
    return c.render('templates/2004-2019年城镇私营与非私营就业人口分布.html')